package com.scorpio.common.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class EchartsUtil {

    public static JSONObject tooltip() {
        return tooltip("axis", "cross", "#999");
    }

    public static JSONObject tooltip(String trigger, String type, String color) {
        JSONObject tooltip = new JSONObject();
        JSONObject axisPointer = new JSONObject();
        JSONObject crossStyle = new JSONObject();

        crossStyle.put("color", color);

        axisPointer.put("type", type);
        axisPointer.put("crossStyle", crossStyle);

        tooltip.put("trigger", trigger);
        tooltip.put("axisPointer", axisPointer);
        return tooltip;
    }

    public static JSONObject tooltip_Pie() {
        JSONObject tooltip = new JSONObject();
        tooltip.put("trigger", "item");
        tooltip.put("formatter", "{a} <br/>{b}: {c} ({d}%)");
        return tooltip;
    }

    public static JSONObject toolbox(String... type) {
        JSONObject toolbox = new JSONObject();
        JSONObject feature = new JSONObject();


        JSONObject dataView = new JSONObject();
        dataView.put("show", true);
        dataView.put("readOnly", false);
        feature.put("dataView", dataView);

        JSONObject magicType = new JSONObject();
        magicType.put("show", true);

        magicType.put("type", type);
        feature.put("magicType", magicType);

        JSONObject restore = new JSONObject();
        restore.put("show", true);
        feature.put("restore", restore);


        JSONObject saveAsImage = new JSONObject();
        saveAsImage.put("show", true);
        feature.put("saveAsImage", saveAsImage);

        toolbox.put("feature", feature);
        return toolbox;
    }

    public static JSONObject legend(String... data) {
        JSONObject legend = new JSONObject();
        legend.put("data", data);
        return legend;
    }

    public static JSONObject xAxis(String type, JSONArray data, String axisPointer_type) {
        JSONObject xAxis = new JSONObject();
        xAxis.put("type", type);
        xAxis.put("data", data);
        JSONObject axisPointer = new JSONObject();
        axisPointer.put("type", axisPointer_type);
        xAxis.put("axisPointer", axisPointer);
        return xAxis;
    }

    public static JSONObject yAxisItem(String type, String name, Integer min, Integer max, Integer interval, String formatter) {
        JSONObject json = new JSONObject();
        json.put("type", type);
        json.put("name", name);
        json.put("min", min);
        json.put("max", max);
        json.put("interval", interval);
        JSONObject axisLabel = new JSONObject();
        axisLabel.put("formatter", formatter);
        json.put("axisLabel", axisLabel);
        return json;
    }

    public static JSONObject seriesItem(String name, String type, JSONArray data, Integer yAxisIndex) {
        JSONObject json = new JSONObject();
        json.put("name", name);
        json.put("type", type);
        json.put("data", data);
        json.put("yAxisIndex", yAxisIndex);
        return json;
    }

    public static JSONObject seriesItem_Pie(String name, String radius,JSONArray data) {
        JSONObject json = new JSONObject();
        json.put("name", name);
        json.put("type", "pie");
        json.put("radius", radius);

        JSONObject label = new JSONObject();
        label.put("formatter", "{a|{a}}{abg|}\n{hr|}\n  {b|{b}：}{c}  {per|{d}%}");
        label.put("backgroundColor", "#eee");
        label.put("borderColor", "#eee");
        label.put("borderWidth", 1);
        label.put("borderRadius", 4);


        JSONObject a = new JSONObject();
        a.put("color", "#999");
        a.put("lineHeight", 22);
        a.put("align", "'center'");

        JSONObject abg = new JSONObject();
        abg.put("abg", "#333");
        abg.put("width", "100%");
        abg.put("align", "right");
        abg.put("height", 22);
        abg.put("borderRadius", new int[]{4, 4, 0, 0});

        JSONObject hr = new JSONObject();
        hr.put("borderColor", "#aaa");
        hr.put("width", "100%");
        hr.put("borderWidth", 0.5);
        hr.put("height", 0);

        JSONObject b = new JSONObject();
        b.put("fontSize", 16);
        b.put("lineHeight", 33);

        JSONObject per = new JSONObject();
        per.put("color", "#eee");
        per.put("backgroundColor", "#334455");
        per.put("padding", new int[]{2, 4});
        per.put("borderRadius", 2);


        JSONObject rich= new JSONObject();
        rich.put("a", a);
        rich.put("abg", abg);
        rich.put("hr", hr);
        rich.put("b", b);

        label.put("rich",rich);

        label.put("per", per);
        json.put("label", label);
        json.put("data", data);
        return json;
    }

    public static int toAverageMax(JSONArray totali) {
        int max = totali.getInteger(0);
        for (int i = 0; i < totali.size(); i++) {
            int item = totali.getInteger(i);
            if (item >= max) max = item;
        }
        if (max < 100) {
            return 100;
        } else if (max > 100 && max <= 500) {
            return 500;
        } else if (max > 500 && max <= 2000) {
            return 2000;
        } else if (max > 2000 && max <= 5000) {
            return 5000;
        } else if (max > 5000 && max <= 10000) {
            return 10000;
        } else if (max > 10000 && max <= 50000) {
            return 50000;
        } else if (max > 50000 && max <= 100000) {
            return 100000;
        } else if (max > 50000 && max <= 100000) {
            return 100000;
        } else if (max > 100000 && max <= 1000000) {
            return 1000000;
        } else if (max > 1000000 && max <= 10000000) {
            return 10000000;
        }
        return 10000000;
    }

    public static int toAverageMin(JSONArray totali) {
        int min = totali.getInteger(0);
        for (int i = 0; i < totali.size(); i++) {
            int item = totali.getInteger(i);
            if (min >= item) min = item;
        }
        if (min > 100) {
            return 100;
        } else if (min > 500 && min <= 2000) {
            return 500;
        } else if (min > 2000 && min <= 5000) {
            return 2000;
        } else if (min > 5000 && min <= 10000) {
            return 5000;
        } else if (min > 10000 && min <= 50000) {
            return 10000;
        } else if (min > 50000 && min <= 100000) {
            return 50000;
        } else if (min > 50000 && min <= 100000) {
            return 50000;
        } else if (min > 100000 && min <= 1000000) {
            return 100000;
        } else if (min > 1000000 && min <= 10000000) {
            return 1000000;
        } else if (min > 10000000) {
            return 10000000;
        }
        return 0;
    }
}
